<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>修改题目</title>
</head>
<body>

<?php
require_once ('../config/path_config.php');
require_once (ROOTDIR."config/sys_config.php");
require_once(ROOTDIR."lang/cn.php");
require_once(ROOTDIR."admin/admin-header.php");
require_once(ROOTDIR."common/db_utils.php");

// 权限控制
$allowAccess = isset($_SESSION['administrator']);
$allowAccess |= isset($_SESSION['problem_editor']);
if ( !$allowAccess ) {
    echo "不具备访问该页面的权限";
    exit(1);
}

include_once("kindeditor.php");

// 题目编辑
if ( isset($_GET['id']) ) {
?>
<div class="container">
    <form method=POST action=problem_edit.php>
        <?php
        // 查询题目信息
        $sql = "SELECT * FROM `problem` WHERE `problem_id`=?";
        $result = DbUtil::getInstance()->query($sql, intval($_GET['id']));
        $row = $result[0];
        ?>
        <input type=hidden name=problem_id value='<?php echo $row['problem_id'] ?>'>

        <p>
            题目ID: <?php echo $row['problem_id'] ?>
        </p>
        <p style="color:#FF0000">题目备注示例：&nbsp;NOIP2011TGD1T1 &nbsp;&nbsp;NOIP2011PJT1</p>
        <!--题目标题-->
        <p>
            <?php echo $MSG_TITLE ?>:
            <input class="input-xxlarge" type=text name=title
                   value='<?php echo htmlentities($row['title'], ENT_QUOTES, "UTF-8") ?>'>
        </p>

        <?php echo $MSG_Time_Limit ?>:
            <input class="input-mini" type=text name=time_limit size=20
                   value='<?php echo $row['time_limit'] ?>'>&nbsp;&nbsp;秒&nbsp;&nbsp;
        <?php echo $MSG_Memory_Limit ?>:
            <input class="input-mini" type=text name=memory_limit size=20
                   value='<?php echo $row['memory_limit'] ?>'>&nbsp;&nbsp;MB
        <p>
            <?php echo $MSG_Description ?>:<br>
            <textarea class="kindeditor" rows=13 name=description cols=200>
                <?php echo htmlentities($row['description'], ENT_QUOTES, "UTF-8") ?>
            </textarea>
        </p>
        <p>
            <?php echo $MSG_Input ?>:<br>
            <textarea class="kindeditor" rows=13 name=input cols=200>
                <?php echo htmlentities($row['input'], ENT_QUOTES, "UTF-8") ?>
            </textarea>
        </p>
        <p>
            <?php echo $MSG_Output ?>:<br>
            <textarea class="kindeditor" rows=13 name=output cols=200>
                <?php echo htmlentities($row['output'], ENT_QUOTES, "UTF-8") ?>
            </textarea>
        </p>
        <p>
            <!--样例输入-->
            <?php echo $MSG_Sample_Input ?>:
            <textarea rows=13 name=sample_input cols=120><?php echo htmlentities($row['sample_input'], ENT_QUOTES, "UTF-8");?></textarea>
            <!--样例输出-->
            <?php echo $MSG_Sample_Output ?>:
            <textarea rows=13 name=sample_output cols=120><?php echo htmlentities($row['sample_output'], ENT_QUOTES, "UTF-8");?></textarea>
        </p>
        <!--提示-->
        <p>
            <?php echo $MSG_HINT ?>:<br>
            <textarea class="kindeditor" rows=13 name=hint cols=120><?php echo htmlentities($row['hint'], ENT_QUOTES, "UTF-8");?></textarea>
        </p>
        </p>
        <!--特殊判断-->
        <p>
            <?php echo $MSG_SPJ ?>:
            N<input type=radio name=spj value='0' <?php echo $row['spj'] == "0" ? "checked" : "" ?> >&nbsp;&nbsp;&nbsp;
            Y<input type=radio name=spj value='1' <?php echo $row['spj'] == "1" ? "checked" : "" ?> >
        </p>
        </p>
        <!--所属类别-->
        <div>
            <?php echo $MSG_Source ?>:<br>
            <textarea style="width:400px;"  name=source rows=1 cols=180><?php echo htmlentities($row['source'], ENT_QUOTES, "UTF-8");?></textarea>
        </div>
        <div style="color:blue;">
            分为：程序设计基础、数据结构、基础算法3个类别9个等级。如：程序设计基础(一维数组、冒泡排序)
        </div>
        <div style="border:1px solid #F0F0EE; width: 700px;">
            难度等级: <br>
            <table width="700" height="65">
                <tr><td width="23%">
                        <li style="list-style-type:none;"><input type=radio name=level value='1' <?php echo $row['level'] == "1" ? "checked" : ""?> >1级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='2' <?php echo $row['level'] == "2" ? "checked" : ""?> >2级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='3' <?php echo $row['level'] == "3" ? "checked" : ""?> >3级</li>
                    </td>
                    <td width="20%">
                        <li style="list-style-type:none;"><input type=radio name=level value='4' <?php echo $row['level'] == "4" ? "checked" : ""?> >4级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='5' <?php echo $row['level'] == "5" ? "checked" : ""?> >5级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='6' <?php echo $row['level'] == "6" ? "checked" : ""?> >6级</li>
                    </td>
                    <td width="20%">
                        <li style="list-style-type:none;"><input type=radio name=level value='7' <?php echo $row['level'] == "7" ? "checked" : ""?> >7级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='8' <?php echo $row['level'] == "8" ? "checked" : ""?> >8级</li>
                        <li style="list-style-type:none;"><input type=radio name=level value='9' <?php echo $row['level'] == "9" ? "checked" : ""?> >9级</li>
                    </td>
                </tr>
            </table>
        </div>
        <!--题目来源-->
        <p>
            题目来源:<br>
            <textarea style="width:400px;" name=fromoj rows=1 cols=180><?php echo htmlentities($row['fromoj'], ENT_QUOTES, "UTF-8");?></textarea>
        </p>
        <!--
        <p> here
            解题思路(博客链接):<br>
            <textarea name=ans_url style="rows:1; width: 700px;"><?php echo htmlentities($row['ans_url'], ENT_QUOTES, "UTF-8");?></textarea>
        </p>
        -->
        <div align=center>
            <?php require_once("../include/set_post_key.php"); ?>
            <input type=submit value=提交 name=submit>
        </div>
    </form>
    <p>

<?php
}
else {
    // 将编辑后的题目入库
    require_once("../include/check_post_key.php");

    // 查询题目的创建者
    $id=intval($_POST['problem_id']);
    $sql = "select `creater_id` from problem where `problem_id`=?";
    $result = DbUtil::getInstance()->query($sql, $id);
    $row = $result[0];

    // 判断用户是否具有编辑权限
    $allowEdit = isset($_SESSION['administrator']);
    if ( !$allowEdit ) {
        $allowEdit = isset($_SESSION['problem_editor'])
            && strtolower($row['creater_id']) == strtolower($_SESSION['user_id']);
    }
    if( !$allowEdit ) {
        exit(0);
    }
    $title=$_POST['title'];
    $time_limit=$_POST['time_limit'];
    $memory_limit=$_POST['memory_limit'];
    $description=$_POST['description'];
    $input=$_POST['input'];
    $output=$_POST['output'];
    $sample_input=$_POST['sample_input'];
    $sample_output=$_POST['sample_output'];
    $hint=$_POST['hint'];
    $source=$_POST['source'];
    $level=$_POST['level'];
    $spj=$_POST['spj'];
    $fromoj = $_POST['fromoj'];
    if ( get_magic_quotes_gpc () ) {
        $title = stripslashes($title);
        $time_limit = stripslashes($time_limit);
        $memory_limit = stripslashes($memory_limit);
        $description = stripslashes($description);
        $input = stripslashes($input);
        $output = stripslashes($output);
        $sample_input = stripslashes($sample_input);
        $sample_output = stripslashes($sample_output);
        $hint = stripslashes($hint);
        $fromoj = stripslashes($fromoj);
    }

    $basedir=SysConfig::$OJ_DATA."/$id";
    // 创建样例文件夹
    if ( ! file_exists ( $basedir) ) {
        mkdir($basedir);
    }
    echo "Sample data file in $basedir Updated!<br>";
    if( $sample_input ) {
        $input_file = $basedir."/sample.in";
        $fp = fopen($input_file,"w");
		fputs($fp,preg_replace("(\r\n)","\n",$sample_input));
		fclose($fp);
		
		$fp=fopen($basedir."/sample.out","w");
		fputs($fp,preg_replace("(\r\n)","\n",$sample_output));
		fclose($fp);
	}

	$spj=intval($spj);

    $sql="UPDATE `problem` set `title`=?,`time_limit`=?,`memory_limit`=?, `description`=?,
            `input`=?,`output`=?,`sample_input`=?,`sample_output`=?,`hint`=?,`source`=?, `level`=?,`spj`=?,`in_date`=NOW(), `fromoj`=?
              WHERE `problem_id`=?";
    DbUtil::getInstance()->query($sql,
        $title, $time_limit, $memory_limit, $description,
        $input, $output,  $sample_input, $sample_output,
        $hint,  $source,  $level, $spj, $fromoj, $id) ;
    echo "Edit OK!";
    echo "<a href='../problem.php?id=$id'>See The Problem!</a>";
}
?>
</div>
</body>
</html>